package com.shop.dao.impl;

import com.shop.dao.UserDao;
import com.shop.entity.User;
import com.shop.exception.DAOException;
import com.shop.transaction.TransactionManager;
import com.shop.util.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    public User userRex(String name, String pwd) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "select * from user where u_name=? and u_pwd=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,pwd);
            rs = ps.executeQuery();
            if(rs.next()) {
                user = new User();
                user.setU_id(rs.getInt("u_id"));
                user.setU_name(name);
                user.setU_authority(rs.getInt("u_authority"));
                user.setU_age(rs.getInt("u_age"));
                user.setU_sex(rs.getString("u_sex"));
                user.setU_location(rs.getString("u_location"));
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return user;
    }

    @Override
    public int register(User user) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "insert into user(u_name,u_pwd,u_authority,u_age,u_sex,u_location) values(?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getU_name());
            ps.setString(2,user.getU_pwd());
            ps.setInt(3,user.getU_authority());
            ps.setInt(4,user.getU_age());
            ps.setString(5,user.getU_sex());
            ps.setString(6,user.getU_location());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return result;
    }

    @Override
    public User selectUserByName(String u_name) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "select * from user where u_name=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,u_name);
            rs = ps.executeQuery();
            if(rs.next()) {
                user = new User();
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return user;
    }
}
